from tkinter import *
import os
import sys
token = ""
threeInstr = ""
table = ""
yufa = ""
import time

def run1():
    global token,yufa,table,threeInstr
    src = lb.get('0.0','end')
    with open("srcinput.txt","w") as f:
        f.write(src)
    # time.sleep(0.1)
    # string = ""
    # Stdout = sys.stdout
    # with open("tmp-out.txt","w") as f:
    #     sys.stdout = f
    # sys.stdout = string
    os.system("test1 > tmp-out.txt")
    with open("tmp-out.txt","r",encoding="utf-8") as f:
        # print(f.read())
        token = f.read()
        token.replace("token is ","")
    os.remove("tmp-out.txt")
    os.system("test3 > tmp-out.txt")
    with open("tmp-out.txt","r",encoding="utf-8") as f:
        # print(f.read())
        string = f.read()
        l = string.split("\n########################\n")
        try:
            yufa = l[1]
        except:
            yufa = "语法分析失败"
        try:
            threeInstr = l[2]
        except:
            threeInstr = "运行失败"
        try:
            table = l[3]
        except:
            table = "获取符号表失败"
        # threeInstr = l[2]

        
        # for i in range(len(l)):




    # print(token)
        # print("nihao ")
    # os.system("a")

    # os.system("1 ")

def run2():
    global token
    lb2.delete('1.0','end')
    lb2.insert('end',token)
    # os.system("1 ")
def run3():
    global yufa
    lb2.delete('1.0','end')
    lb2.insert('end',yufa)
def run4():
    global threeInstr
    lb2.delete('1.0','end')
    lb2.insert('end',threeInstr)
def run5():
    global table
    lb2.delete('1.0','end')
    lb2.insert('end',table)

root= Tk()
root.title('编译原理')
root.iconbitmap("favicon.ico")
var=StringVar()
lab1 = Label(root,
                 # 背景选项
                 # background="blue",
                 # 文本
                 text = "请输入源程序",
                 )
lab1.place(x=20, y=0,relwidth=0.4,height=50)
lab2 = Label(root,
                 #背景选项
                 # background="blue",
                 #文本
                 text = "结果显示",
                 )
lab2.place(relx=0.6, y=0,relwidth=0.3,height=50)

lb = Text(root)
text = """int a;
int b;
a = 1;
b = 1;
if(a>b)
a = a - 1;
else
b = b-1;"""
lb.insert('end', text)
# scrollbarx = Scrollbar(root,orient=HORIZONTAL) 
# scrollbary = Scrollbar(root)

# lb['xscrollcommand'] = scrollbarx.set
# lb['yscrollcommand'] = scrollbary.set
# #side指定Listbox为居左,
lb.place(x=20, rely=0.1, relwidth=0.4, relheight=0.7)

lb2 = Text(root)
lb2.place(relx = 0.5,rely=0.1,relwidth=0.45,relheight=0.7)


# scrollbary['command'] = lb.yview  
# scrollbarx['command'] = lb.xview  

# inp1 = Entry(root)
# inp1.place(relx=0.6, rely=0.2, \
#     relwidth=0.3, relheight=0.1)

# 方法-直接调用 run1()
btn1 = Button(root, text='编译', command=run1)
btn1.place(relx=0.05, rely=0.83, \
    relwidth=0.1, relheight=0.1)
# 方法-直接调用 run1()
btn1 = Button(root, text='词法分析', command=run2)
btn1.place(relx=0.2, rely=0.83, \
    relwidth=0.15, relheight=0.1)
# 方法-直接调用 run1()
btn1 = Button(root, text='语法分析', command=run3)
btn1.place(relx=0.4, rely=0.83, \
    relwidth=0.15, relheight=0.1)
# 方法-直接调用 run1()
btn1 = Button(root, text='中间代码', command=run4)
btn1.place(relx=0.6, rely=0.83, \
    relwidth=0.15, relheight=0.1)
# 方法-直接调用 run1()
btn1 = Button(root, text='符号表', command=run5)
btn1.place(relx=0.8, rely=0.83, \
    relwidth=0.15, relheight=0.1)

# CheckVar1 = IntVar()
# CheckVar2 = IntVar()
# CheckVar1.set(1)
# CheckVar2.set(0)

# pattern1 = Checkbutton(root, text = "模糊查询", variable = CheckVar1, \
#                    onvalue = 1, offvalue = 0, command=lambda :choosePattern(1))
# pattern2 = Checkbutton(root, text = "精确查询", variable = CheckVar2, \
#                    onvalue = 1, offvalue = 0, command=lambda :choosePattern(2))
# pattern1.place(relx=0.68,rely=0.6,relwidth=0.2,relheight=0.1)
# pattern2.place(relx=0.68,rely=0.7,relwidth=0.2,relheight=0.1)
# 这里的乘号不是 * ，而是小写英文字母 x
root.geometry('600x400') 
root.mainloop()